using System.Web;
using LinkPointTransaction;
using System.Text;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Configuration;
using SiteManager;
using System.Data.SqlClient;


public partial class status : System.Web.UI.Page
{

    private void Page_Load(object sender, System.EventArgs e)
    {
        // Put user code to initialize the page here
        //Manager mgr = new Manager(true,"ConnectionString");

        //HttpBrowserCapabilities bc = Request.Browser;
        //fIE5 = (bc.Browser == "IE" && bc.MajorVersion > 4);

        //order = (string)Session["outXml"];
        //resp = (bool)Session["resp"];
        //ParseResponse(resp);
        string strError=string.Empty;
        Manager ObjManager = new Manager(true,"ConnectionString");
        int iDoctorid = Convert.ToInt32(Session["userID"]);
        string retcode = Request.Form["Return_Code"];
        string appnumb = Request.Form["Approv_Num"];
        string cardrep = Request.Form["Card_Response"];
        string avsresp = Request.Form["AVS_Response"];
        string couponcode = string.Empty;
        if (!Equals(Session["couponcode"], null))
        {
            couponcode = Convert.ToString(Session["couponcode"]);
        }
        else
        {
            couponcode = "";
        }
       
        if (retcode  == "Y")
        {
            
            //string delSql = "delete from doctorsubscription where DoctorId=" + Session["userID"];
            //mgr.ExecuteScalarQuery(delSql);
            // code for set IsActive false

            ObjManager.ExecuteScalarQuery("Update doctorsubscription set TransactionId='" + appnumb.ToString() + "',SubscriptionId  = '" + appnumb.ToString() + "'  where DoctorId='" + iDoctorid + "'"); 
            ObjManager.ExecuteScalarQuery("update coupons set IsActive='false',UsedAsOn=getdate() where couponcode='" + couponcode.ToString() + "' and Fordoctorid='" + iDoctorid + "'");
            //end code
            lblMsg.Text = "Your Transaction has been successfully processed.";
            lblMsg.ForeColor = System.Drawing.Color.Red;
            lblMsg.Font.Bold = true;
            lblMsg.Font.Size = 10;
        }
        else
        {
            strError += "Our Card Code Verification (CCV) returned " + "the following error: ";
            Doctor objdoct = new Doctor();
              /////////////inayat
            //ObjManager.ExecuteScalarQuery("update tbl_doctors set FeatureDocTypeId ='" + DBNull.Value + "' where id ='" + iDoctorid + "'");
            //ObjManager.ExecuteScalarQuery("Delete from doctorsubscription where DoctorId= '" + iDoctorid + "'");	
            //ObjManager.ExecuteScalarQuery("Delete from tbl_drpremium where dr_id = '" + iDoctorid + "'");
            //objdoct.DeleteDoctorSubscription(iDoctorid);

            ////////////////end////////////////

            // code for set IsActive false
            //ObjManager.ExecuteScalarQuery("update coupons set IsActive='true',UsedAsOn='' where couponcode='" + txtCouponCode.Text.ToString() + "' and Fordoctorid='" + iDoctorid + "'");
            //end code
            switch (avsresp)
            {
                case "A":
                    strError += " the zip code entered does not match " +
                      "the billing address.";
                    break;
                case "B":
                    strError += " no information was provided for the AVS check.";
                    break;
                case "E":
                    strError += " a general error occurred in the AVS system.";
                    break;
                case "G":
                    strError += " the credit card was issued by a non-US bank.";
                    break;
                case "N":
                    strError += " neither the entered street address nor zip " +
                      "code matches the billing address.";
                    break;
                case "P":
                    strError += " AVS is not applicable for this transaction.";
                    break;
                case "R":
                    strError += " please retry the transaction; the AVS system " +
                      "was unavailable or timed out.";
                    break;
                case "S":
                    strError += " the AVS service is not supported by your " +
                      "credit card issuer.";
                    break;
                case "U":
                    strError += " address information is unavailable for the " +
                      "credit card.";
                    break;
                case "W":
                    strError += " the 9 digit zip code matches, but the " +
                      "street address does not.";
                    break;
                case "Z":
                    strError += " the zip code matches, but the address does not.";
                    break;
                
            }
            lblMsg.Text = strError;//retcode + "-" + appnumb + "-" + cardrep + "-" + avsresp;
        }
        //// We need LPTxn to display version info
        //LPTxn = new LinkPointTxn();
    }
    protected void ParseResponse(string rsp)
    {
        R_Time = ParseTag("r_time", rsp);
        R_Ref = ParseTag("r_ref", rsp);
        R_Approved = ParseTag("r_approved", rsp);
        R_Code = ParseTag("r_code", rsp);
        R_Authresr = ParseTag("r_authresronse", rsp);
        R_Error = ParseTag("r_error", rsp);
        R_OrderNum = ParseTag("r_ordernum", rsp);
        R_Message = ParseTag("r_message", rsp);
        R_Score = ParseTag("r_score", rsp);
        R_TDate = ParseTag("r_tdate", rsp);
        R_AVS = ParseTag("r_avs", rsp);
        R_Tax = ParseTag("r_tax", rsp);
        R_Shipping = ParseTag("r_shipping", rsp);
        R_FraudCode = ParseTag("r_fraudCode", rsp);
        R_ESD = ParseTag("esd", rsp);
    }
    protected string ParseTag(string tag, string rsp)
    {
        StringBuilder sb = new StringBuilder(256);
        sb.AppendFormat("<{0}>", tag);
        int len = sb.Length;
        int idxSt = -1, idxEnd = -1;
        if (-1 == (idxSt = rsp.IndexOf(sb.ToString())))
        { return ""; }
        idxSt += len;
        sb.Remove(0, len);
        sb.AppendFormat("</{0}>", tag);
        if (-1 == (idxEnd = rsp.IndexOf(sb.ToString(), idxSt)))
        { return ""; }
        return rsp.Substring(idxSt, idxEnd - idxSt);
    }
    protected LinkPointTxn LPTxn;
    protected string order;
    protected string resp;
    protected bool fIE5;
    protected string R_Time;
    protected string R_Ref;
    protected string R_Approved;
    protected string R_Code;
    protected string R_Authresr;
    protected string R_Error;
    protected string R_OrderNum;
    protected string R_Message;
    protected string R_Score;
    protected string R_TDate;
    protected string R_AVS;
    protected string R_FraudCode;
    protected string R_ESD;
    protected string R_Tax;
    protected string R_Shipping;

}
